$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'user/userinfos/list',
        datatype: "json",
        colModel: [			
			{ label: '用户ID', name: 'userinfoid', index: 'UserInfoId', width: 70, key: true },
			{ label: '用户账户', name: 'useraccount', index: 'UserAccount', width: 80 },
			{ label: '用户名', name: 'username', index: 'UserName', width: 80 }, 			
			{ label: '性别', name: 'usergender', index: 'UserGender',align:"center", width: 50,formatter: function(value, options, row){
                    return value === 0 ? '男' : '女';
                } },
			{ label: '手机号码', name: 'phonenum', index: 'PhoneNum', width: 80 },
			{ label: '积分余额', name: 'integralnumber', index: 'IntegralNumber', width: 80 },
			{ label: '用户地址', name: 'addressinfo', index: 'AddressInfo', width: 80 },
			{ label: '注册时间', name: 'createtime', index: 'CreateTime', width: 90 },
			{ label: '是否Vip', name: 'isvip', index: 'IsVip', width: 50 ,formatter: function(value, options, row){
                    if(value === 0){
						return  '<span class="label label-info">普通用户</span>';
                    }else if(value === 1){
                       return  '<span class="label label-primary">VIP用户</span>';
					}else{
                        return '<span class="label label-danger">错误</span>';
					}
                }},
			{ label: '用户锁定状态', name: 'islockedout', index: 'IsLockedOut', width: 80,formatter: function(value, options, row){
                    if(value === 0){
                        return  '<span class="label label-success">正常</span>';
                    }else if(value === 1){
                        return  '<span class="label label-danger">已锁定</span>';
                    }else{
                        return '<span class="label label-danger">错误</span>';
                    }
    			} },
			{ label: '软删除', name: 'isdel', index: 'IsDel', width: 50 ,formatter: function(value, options, row){
                    if(value === 0){
                        return  '<span class="label label-success">正常</span>';
                    }else if(value === 1){
                        return   '<span class="label label-danger">已删除</span>';
                    }else{
                        return '<span class="label label-danger">错误</span>';
                    }
                }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        searchParam:null,
        isdisabled:null,
        usergendertypes:[],
		showList: true,
		title: null,
		userinfos: {},
        usersetdatatext:{},
        uploadBase64:null
	},
	beforeMount:function () {
		//查询数据字典性别类型数据
        $.ajax({
            type: "GET",
            url: baseURL + 'dictionary/dictionary/searchByTypename?typename=sex',
            contentType: "application/json",
            success: function(r){
                if(r.code === 0){
                    vm.usergendertypes = r.data;
                }else{

                }
            }
        });
    },
	methods: {
		query: function () {
           vm.reload();
		},
		add: function(){
            vm.isdisabled = null;
			vm.showList = false;
			vm.title = "新增";
            vm.uploadBase64 = null;
			vm.userinfos = {islockedout:'0',isdel:'0',isvip:'0',usergender:'0',userheadpic: ''};
		},
		update: function (event) {
			var userinfoid = getSelectedRow();
			if(userinfoid == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.isdisabled= 'disabled';
            
            vm.getInfo(userinfoid)
		},
		saveOrUpdate: function (event) {
			var url = vm.userinfos.userinfoid == null ? "user/userinfos/save" : "user/userinfos/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.userinfos),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var userinfoids = getSelectedRows();
			if(userinfoids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "user/userinfos/delete",
                    contentType: "application/json",
				    data: JSON.stringify(userinfoids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(userinfoid){
			$.get(baseURL + "user/userinfos/info/"+userinfoid, function(r){
                vm.userinfos = r.userinfos;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                url:baseURL + "user/userinfos/queryUser" + ((vm.searchParam != null)? "?searchParam=" + vm.searchParam:""),
                page:page
            }).trigger("reloadGrid");
			vm.searchParam = null;
		}
	}
});

layui.use('upload', function(){
    var upload = layui.upload;

    //执行实例
    var uploadInst = upload.render({
        auto:false,
        bindAction:start,
        elem: '#upload' //绑定元素
        ,url: '/upload?type=0' //上传接口
        ,field: 'myFileName',
        accept:'images/jpg, image/png',
        acceptMime: 'image/jpg, image/png'
        ,done: function(res){
            //设置url
            vm.userinfos.userheadpic = res.data;
            alert("上传成功!");
        }
        ,error: function(){
            alert("上传成功!");
        },choose:function (obj) {
            obj.preview(function(index, file, result){
                //预览
                vm.userinfos.userheadpic = null;
                vm.uploadBase64 = result;
            });
        }
    });
});